package com.mybatisplus.controller;

import com.mybatisplus.entity.BaseResultBean;
import com.mybatisplus.entity.Student;
import com.mybatisplus.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.LinkedHashSet;

/**
 * Auto created By IntelliJ IDEA
 *
 * @author : gzrong
 * @Date : 2023/5/6 22:27
 */
@RestController
@RequestMapping("/studentRob")
public class RobController {
    @Autowired
    StudentService studentService;
    private final LinkedHashSet<Student> set = new LinkedHashSet<>();
    private boolean isSigning = false;

    @RequestMapping("/getRob")
    public Object[] getRob() {
        System.out.println(Arrays.toString(set.toArray()));
        if (isSigning) {
            return set.toArray();
        } else {
            return null;
        }
    }

    @RequestMapping("/stuRob")
    public BaseResultBean stuRob(HttpServletRequest request) {
        BaseResultBean bean = new BaseResultBean();
        if (isSigning) {
            if (set.add((Student) request.getSession().getAttribute("student"))) {
                bean.setMessage("抢答成功");
                bean.setStatus(true);
            } else {
                bean.setMessage("抢答失败");
                bean.setStatus(false);
            }
        } else {
            bean.setMessage("抢答未开始");
            bean.setStatus(false);

        }
        return bean;
    }

    @GetMapping("/startAnswer")//开始抢答
    public BaseResultBean startAnswer(HttpServletRequest request) {
        BaseResultBean bean = new BaseResultBean();
        set.clear();
        if (request.getSession(false) != null) {
            if (!isSigning) {
                isSigning = true;
                bean.setStatus(true);
                bean.setMessage("抢答发起成功");
            } else {
                bean.setStatus(true);
                bean.setMessage("正在进行抢答，请勿重复发起");
            }
        } else {
            bean.setMessage("登录失效，请重新登录");
            bean.setStatus(false);
        }
        return bean;
    }

    @GetMapping("/stopAnswer")//停止抢答
    public BaseResultBean stopAnswer(HttpServletRequest request) {
        BaseResultBean bean = new BaseResultBean();
        if (request.getSession(false) == null) {
            isSigning = false;
            bean.setStatus(false);
            bean.setMessage("登陆失效,请重新登录");
        } else {
            if (isSigning) {
                isSigning = false;
                System.out.println("已停止抢答");
                bean.setMessage("已停止抢答");
                bean.setStatus(true);
            } else {
                bean.setMessage("抢答未开始！");
                bean.setStatus(false);
            }
        }

        return bean;
    }

    @RequestMapping("/getStatus")
    public boolean getStatus() {
        return isSigning;
    }
}
